package com.zhang.zhangapp.bean.location;

import android.database.Cursor;

import com.zhang.zhangapp.application.MApplication;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/12.
 */

public class CityDao {

    public static void saveAll(List<City> cityList) {
        try {
            x.getDb(MApplication.getInstance().getDaoConfig()).saveOrUpdate(cityList);
            x.getDb(MApplication.getInstance().getDaoConfig()).close();
        } catch (Exception e) {

        }
    }

    public static City queryCityByName(String cityName) {
        City city = null;
        try {
            Cursor cursor = x.getDb(MApplication.getInstance().getDaoConfig()).execQuery("select * from china_city where name = \'" + cityName + "\'");
            while (cursor.moveToNext()) {
                city = new City();
                //String clat, String initial, String clon, String name, String py, String code, String flag
                city.setClat(cursor.getString(cursor.getColumnIndex("clat")));
                city.setClon(cursor.getString(cursor.getColumnIndex("clon")));
                city.setCode(cursor.getString(cursor.getColumnIndex("code")));
                city.setFlag(cursor.getString(cursor.getColumnIndex("flag")));
                city.setInitial(cursor.getString(cursor.getColumnIndex("initial")));
                city.setName(cursor.getString(cursor.getColumnIndex("name")));
                city.setPy(cursor.getString(cursor.getColumnIndex("py")));
            }
            cursor.close();
            x.getDb(MApplication.getInstance().getDaoConfig()).close();
        } catch (Exception e) {

        }
        return city;
    }

    public static String queryCityCode(String cityName) {
        try {
            Cursor cursor = x.getDb(MApplication.getInstance().getDaoConfig()).execQuery("select * from china_city where name like \"%" + cityName + "%\"");
            City city;
            while (cursor.moveToNext()) {
                String code = cursor.getString(cursor.getColumnIndex("code"));
                return code;
            }
            cursor.close();
            x.getDb(MApplication.getInstance().getDaoConfig()).close();
        } catch (Exception e) {
        }
        return null;
    }

    public static List<City> queryCity(String keyword) {
        try {
            Cursor cursor = x.getDb(MApplication.getInstance().getDaoConfig()).execQuery("select * from china_city where name like \"%" + keyword + "%\" or py like \"%" + keyword + "%\"");
            List<City> cityList = new ArrayList<>();
            City city;
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String py = cursor.getString(cursor.getColumnIndex("py"));
                String clat = cursor.getString(cursor.getColumnIndex("clat"));
                String initial = cursor.getString(cursor.getColumnIndex("initial"));
                String code = cursor.getString(cursor.getColumnIndex("code"));
                String flag = cursor.getString(cursor.getColumnIndex("flag"));
                String clon = cursor.getString(cursor.getColumnIndex("clon"));
                city = new City(clat, initial, clon, name, py, code, flag);
                cityList.add(city);
            }
            cursor.close();
            x.getDb(MApplication.getInstance().getDaoConfig()).close();
            return cityList;
        } catch (Exception e) {
        }
        return null;
    }

    public static ArrayList<City> queryAllCity() {
        try {
            Cursor cursor = x.getDb(MApplication.getInstance().getDaoConfig()).execQuery("select * from china_city");
            ArrayList<City> cityList = new ArrayList<>();
            City city;
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String py = cursor.getString(cursor.getColumnIndex("py"));
                String clat = cursor.getString(cursor.getColumnIndex("clat"));
                String initial = cursor.getString(cursor.getColumnIndex("initial"));
                String code = cursor.getString(cursor.getColumnIndex("code"));
                String flag = cursor.getString(cursor.getColumnIndex("flag"));
                String clon = cursor.getString(cursor.getColumnIndex("clon"));
                city = new City(clat, initial, clon, name, py, code, flag);
                cityList.add(city);
            }
            cursor.close();
            x.getDb(MApplication.getInstance().getDaoConfig()).close();
            return cityList;
        } catch (Exception e) {
        }
        return null;
    }

}
